package com.dl7.mvp.module.video.list;

import com.dl7.mvp.api.RetrofitService;
import com.dl7.mvp.local.table.VideoInfo;
import com.dl7.mvp.module.base.IBasePresenter;
import com.dl7.mvp.module.base.ILoadDataView;
import com.dl7.mvp.widget.EmptyLayout;
import com.orhanobut.logger.Logger;

import java.util.List;

import rx.Subscriber;
import rx.functions.Action0;

/**
 * Created by long on 2016/10/11.
 */

public class RecommendPresenter implements IBasePresenter {
    final private ILoadDataView mView;

    public RecommendPresenter(ILoadDataView view) {
        this.mView = view;
    }

    @Override
    public void getData() {
        // 获取推荐的专辑
        Subscriber subscriber0 = new Subscriber<List<VideoInfo>>() {
            @Override
            public void onCompleted() {

            }

            @Override
            public void onError(Throwable e) {
                Logger.e(e.toString());
            }

            @Override
            public void onNext(List<VideoInfo> videoList) {
                mView.loadMoreData(videoList);
            }
        };
        RetrofitService.getRecommendAlbumsList(0, 16)
                .compose(mView.bindToLife())
                .subscribe(subscriber0);

        // 获取推荐视频列表
        Subscriber subscriber = new Subscriber<List<VideoInfo>>() {
            @Override
            public void onCompleted() {
                mView.hideLoading();
            }

            @Override
            public void onError(Throwable e) {
                Logger.e(e.toString());
                mView.showNetError(new EmptyLayout.OnRetryListener() {
                    @Override
                    public void onRetry() {
                        getData();
                    }
                });
            }

            @Override
            public void onNext(List<VideoInfo> videoList) {
                mView.loadData(videoList);
            }
        };

        RetrofitService.getRecommendedVideoList(0, 999)
                .doOnSubscribe(new Action0() {
                    @Override
                    public void call() {
                        mView.showLoading();
                    }
                })
                .compose(mView.bindToLife())
                .subscribe(subscriber);
    }

    @Override
    public void getMoreData() {
    }

}
